#!/bin/bash


# == FS - input field separator ==

# the default field seperator: " ", it matches one or more whitespace
# 1. use -F 'new seperator' option to specify a new one 
# 2. in BEGIN Block, use FS="new seperator" to specify a new one 

# - print employee name and title
awk -F ',' '{print $2,": ", $3}' ./test_employee.txt
echo -e '\n'

awk 'BEGIN {FS=","} {print $2,": ", $3}' ./test_employee.txt
echo -e '\n'

# specify multiple seperator
# 1. in BEGIN Block, use FS="[...]"
awk 'BEGIN {FS="[,:%]"} {print $2, ": ", $3, ": ", $4}' \
    ./test_employee-multiple-fs.txt
echo -e '\n'


# == OFS - output field seperator == 

# we use manual seperator text specification to seperate multiple fields
# at above. its quite annoying. A simple way is to specify OFS

# 1. in BEGIN Block, use OFS='seperator' to specify a new one  
awk -F ',' 'BEGIN {OFS=":"} \
    {print $2, $3}' ./test_employee.txt;
echo -e '\n'


# == RS - record seperator ==
# the simpler refer that refers to IRS (input Record Seperator)
# the test sample in this section: ./test_employee_one_line.txt
# all records saved in one line.
# every record in test sample has 2 field seperated by ',' 
# and records itself are seperated by ':'

# 1. in BEGIN Block, use RS="seperator" to sepcify a new one 
awk -F, 'BEGIN {RS=":"} \
    {print $2}' ./test_employee_one_line.txt
echo -e '\n'

# the test sample in this section: ./test_employee_change_fs_ofs.txt
# records are seperated by '-'
# each field occupy 1 line 

# 2. dealling with multi-line records
awk 'BEGIN {FS="\n"; RS="-\n"; OFS=":"} \
    {print $2, $3}' ./test_employee_change_fs_ofs.txt
echo -e '\n'

 
# == ORS - Output Record Seperator ==

awk 'BEGIN {FS=","; OFS=":"; ORS="\n---\n"} \
    {print $2, $3}' ./test_employee.txt
echo -e '\n'


# == NR - Number of Records ==
# when NR used inside the loop, NR gives the line number.
# when NR used in END Block, it returns the total number of records

# - exmaples
awk 'BEGIN {FS=","} \
    {print "Emp Id of record number", NR, "is", $1;} \
    END {print "total number of records:", NR}' ./test_employee.txt
echo -e '\n'


# == FILENAME - current file name == 
# this variable is helpful when you are secifying multiple input-files to the awk program.
# This will give you the name of the file Awk is current processing

awk '{ print FILENAME } ' \
    ./test_employee.txt ./test_employee_multiple_fs.txt
echo -e '\n'

# when you read input from standard input, FILENAME variable will be set to "-"
echo John Doe | awk '{ print "Last Name: ", $2; print "Filename: ",FILENAME}'
echo -e '\n'


# == FNR - File "Number of Record" ==
# NR: keep growing between multiple files.
# FNR: restart counting when switching input files.

awk 'BEGIN {FS=","} \
    {print FILENAME ": record number", FNR, "is", $1;} \
    END {print "Total number of records:", NR}' \
    ./test_employee.txt ./test_employee_multiple_fs.txt
echo -e '\n'


# == NF - number of fields ==
# NF saves the number of fields of current record
# NF returns the number , $NF returns the last field of current records
awk -F ',' '{print NF}' ./test_employee_sal.txt
echo -e '\n'


